﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_thongtinNV : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            danhsach(0);
        }
    }
    protected void danhsach(int index)
    {
        clsTableAction clsTable = new clsTableAction("NhanVien");
        clsTable.exportDataToGridview("*", "", ref GridView1, 5, index);
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        danhsach(e.NewPageIndex);
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        danhsach(GridView1.PageIndex);
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        danhsach(GridView1.PageIndex);
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        clsTableAction clsTable = new clsTableAction("NhanVien");
        string id = GridView1.Rows[GridView1.EditIndex].Cells[0].Text;
        TextBox txt1 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[1].Controls[0];
        string lableVi2 = txt1.Text;
        TextBox txt2 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[2].Controls[0];
        string lableVi3 = txt2.Text;
        TextBox txt3 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[3].Controls[0];
        string lableVi4 = txt3.Text;
        TextBox txt4 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[4].Controls[0];
        string lableVi5 = txt4.Text;
        TextBox txt5 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[5].Controls[0];
        string lableVi6 = txt5.Text;
        TextBox txt6 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[6].Controls[0];
        string lableVi7 = txt6.Text;
        string sql = string.Format("update NhanVien set HoTen='{0}', GioiTinh ='{1}', NgaySinh = '{2}', Sdt='{3}', Email='{4}', MaPB='{5}' where MaNV='{6}'", txt1.Text, txt2.Text, txt3.Text, txt4.Text, txt5.Text, txt6.Text, id);
        if (clsTable.runSQL(sql))
        {
            GridView1.EditIndex = -1;
            danhsach(GridView1.PageIndex);
            tbl.Text = "Bạn sữa thành công";
        }
        else { tbl.Text = "that bai"; }
        clsTable.free();
    }
    protected void ThemNV_Click(object sender, EventArgs e)
    {
        clsTableAction clsTable = new clsTableAction("NhanVien");
        string str = string.Format("select * from NhanVien where MaNV='" + TextBox1.Text + "'");
        bool kq = false;
        kq = clsTable.ktrunSQL(str);
        if (kq == false)
        {
            Label8.Text = "Mã Nhân Viên đã tồn tại";
            TextBox1.Text = "";
            return;
        }
        string sql = string.Format("insert into NhanVien values('{0}',N'{1}','{2}','{3}','{4}','{5}','{6}');", TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text, TextBox6.Text, TextBox7.Text);
        if (clsTable.runSQL(sql))
        {
            GridView1.EditIndex = -1;
            danhsach(0);
            Label8.Text = "Thêm thông tin Nhân Viên thành công !";
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox4.Text = "";
            TextBox3.Text = "";
            TextBox5.Text = "";
            TextBox6.Text = "";
            TextBox7.Text = "";
        }
        else
        {
            Label8.Text = "Bạn thêm thất bại rồi !";
        }
    }
    protected void XoaNV_Click(object sender,EventArgs e)
    {
        clsQueryList clsQuery = new clsQueryList();
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[6].FindControl("chkXoa");
           //CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[9].FindControl("chkXoa");
            if (chk.Checked)
            {
                string id = GridView1.Rows[i].Cells[0].Text;
                string sql = "delete from NhanVien where MaNV='" +id+"'";
                clsQuery.addSQL(sql);
            }
        }
        if (clsQuery.Execute())
        {
            danhsach(GridView1.PageIndex);
            tbl.Text = "Bạn đã xóa thành công";
        }
        else
        {
            tbl.Text="Xóa thất bại rồi !";
        }
        clsQuery.free();
    }
}